package com.yoomiito.app.present.home;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseLazyFragmentPresenter;
import com.yoomiito.app.model.bean.GoodsInfo;
import com.yoomiito.app.model.bean.NavInfo;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.fragment.HomeNavFragment;
import com.yoomiito.app.utils.NetworkUtils;

import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;

public class HomeNavPresenter extends BaseLazyFragmentPresenter<HomeNavFragment> {

    public HomeNavPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void getNav(String navId){
        if(NetworkUtils.checkNetPermission(getV().getContext())){
            getV().finishRefresh();
            return;
        }
        YmApi.getYmService()
                .getNavigates(navId)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<List<NavInfo>>(handleException()) {
                    @Override
                    public void onSuccess(List<NavInfo> navInfos) {
                        getV().setNavData(navInfos);
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    public void searchGoods(String key, String sort, int hasCoupon,int page){
        if(NetworkUtils.checkNetPermission(getV().getContext())){
            getV().finishRefresh();
            return;
        }
        YmApi.getYmService()
                .searchGoods(key, sort, hasCoupon,page)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<List<GoodsInfo>>getApiTransformerData())
                .compose(XApi.<List<GoodsInfo>>getScheduler())
                .subscribe(new ApiSubscriber<List<GoodsInfo>>(handleException()) {
                    @Override
                    public void onSuccess(List<GoodsInfo> results) {
                        getV().dismissLoading();
                        getV().setData(results);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                    }
                });
    }

}

